home *** CD-ROM | disk | FTP | other *** search
/ Cream of the Crop 1 / Cream of the Crop 1.iso / PROGRAM / SOUNDC.ARJ / SOUNDER.C < prev    next >
C/C++ Source or Header  |  1992-07-10  |  4KB  |  132 lines

  1. /* sound generation and timing interrupt
  2.  *
  3.  * Last change: 10 July 92  JMG
  4.  *
  5.  * Written by:
  6.  *
  7.  *   Nels Anderson
  8.  *   92 Bishop Drive
  9.  *   Framingham, MA  01701
  10.  *
  11.  * Translated to (Borland) C by:
  12.  *
  13.  *   John Gallant
  14.  *   1249 Cedar Creek Circle
  15.  *   Dayton OH 45459
  16.  *
  17.  * Released to the public domain
  18.  */
  19.  
  20.  
  21. #include <stdio.h>
  22. #include <stdlib.h>
  23. #include <dos.h>
  24. #include "\sound\sounder.h"
  25.  
  26.  
  27. char SoundSpeed;    /* multiplier used to slow down sounds*/
  28. char SoundCount;    /* counts how long current sound has been on*/
  29. char far *MySound;        /* points to array of notes and durations*/
  30. #if CPPStyle
  31. void interrupt (*New1CInt)(...);    /* address of new interrupt*/
  32. void interrupt (*Int1CSave)(...);    /* saves original $1C interrupt*/
  33. #else
  34. void interrupt (*New1CInt)();    /* address of new interrupt*/
  35. void interrupt (*Int1CSave)();    /* saves original $1C interrupt*/
  36. #endif
  37. int NumRepeats;        /* number of times to repeat sound*/
  38. int MyClock;        /* general purpose timer*/
  39. int SoundOff;        /* offset into note array*/
  40. char SndFlg;        /* set when sounds allowed*/
  41. char MakeSound;        /* set while sound is going*/
  42.  
  43.  
  44. /* functions *******************************************/
  45.  
  46. void far InitSound(void)
  47. /* initializes and starts the sound controller (but not the sound)
  48.  * the controller uses the timer interrupt (1C)
  49.  */
  50. {
  51.   SndFlg = TRUE;        /* sounds are allowed */
  52.   MakeSound = FALSE;        /* sound initially off */
  53.   MyClock = 0;            /* reset timer */
  54.   New1CInt = TimerInt;        /* get address of interrupt */
  55. }
  56.  
  57.  
  58. void far StartSound(char far Notes[], int Repeats, char Speed)
  59. /*  Start generating the sound pointed to by Notes */
  60. {
  61.   SoundSpeed = Speed;        /* set speed */
  62.   SoundOff = 0;            /* offset into sound array */
  63.   SoundCount = 1;        /* counter for current note */
  64.   MySound = Notes;        /* pointer to sound array */
  65.   NumRepeats = Repeats;        /* number times to repeat sound */
  66.   MakeSound = TRUE;        /* enable sounds */
  67. } /* StartSound() */
  68.  
  69.  
  70. #if CPPStyle
  71. void interrupt TimerInt(...)
  72. #else
  73. void interrupt TimerInt()
  74. #endif
  75. /*  Clock tick interrupt
  76.  *
  77.  * BIOS interrupt 0x1C has been replaced with the following routine.  This
  78.  * interrupt occurs on each clock tick (18 per second).
  79.  *
  80.  * The interrupt mainly handles sounds.  When the MakeSound flag is true,
  81.  * the pointer MySound must be pointing to a byte array containing durations
  82.  * and frequencies of sounds to be generated.  Sounds will be generated from
  83.  * the array until a duration of 0 is found.
  84.  *
  85.  * A general purpose timer is also incremented each time the interrupt
  86.  * occurs.
  87.  *
  88.  * To use the interrupt, the main program needs to do the following:
  89.  *
  90.  * {
  91.  *   GetIntVec(0x1C,Int1CSave);        (save original interrupt vector)
  92.  *   SetIntVec(0x1C,New1CInt);        (install timer interrupt)
  93.  *       .
  94.  *       .                (body of program)
  95.  *       .
  96.  *   StartSound(PhaserSound,3,1);    (phaser sound 3 times, normal speed)
  97.  *       .
  98.  *       .                (body of program)
  99.  *       .
  100.  *   SetIntVec(0x1C,Int1CSave);        (restore original 1C interrupt)
  101.  * }
  102.  *
  103.  */
  104. {
  105.   MyClock++;                /* increment timer */
  106.   if (!SndFlg) {             /* exit if sounds turned off */
  107.     MakeSound = FALSE;
  108.   } else if (MakeSound) {        /* if making a sound...*/
  109.     SoundCount--;
  110.     if (SoundCount <= 0) {         /* if current sound done...*/
  111.       nosound();
  112.       SoundCount = SoundSpeed*MySound[SoundOff];    /* get duration of next one*/
  113.       if (SoundCount > 0) {         /* if there is a next one...*/
  114.     SoundOff++;
  115.     sound(10*MySound[SoundOff]);    /* start it up */
  116.     SoundOff++;
  117.       } else {                 /* if end of sound array...*/
  118.     NumRepeats--;            /* decrement number of repeats */
  119.     if (NumRepeats > 0) {         /* if we must repeat...*/
  120.       SoundOff = 2;            /* reset offset into array */
  121.       SoundCount = MySound[0];    /* get duration of first note */
  122.       sound(10*MySound[1]);        /* start it up */
  123.     } else {            /* if all repeats now done... */
  124.       nosound();            /* stop all sound */
  125.       MakeSound = FALSE;        /* reset flag */
  126.     }
  127.       }
  128.     }  /* if SoundCount = 0 */
  129.   }  /* if making a sound */
  130. }  /* TimerInt() */
  131.  
  132.